package com.atguigu.com.nio.nio_2_channel;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author: robert.song
 * @Date: Created in 2021/8/7
 */
public class NIOFileChannel03 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("D:\\file\\project\\cx\\test.log");
        FileChannel fileChannel = fileInputStream.getChannel();
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\file\\project\\cx\\test-copy.log");
        FileChannel fileChannel1 = fileOutputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(512);
        while (true) {
            byteBuffer.clear();
            int read = fileChannel.read(byteBuffer);
            System.out.println("read=" + read);
            if (read == -1) {
                break;
            }
            byteBuffer.flip();
            fileChannel1.write(byteBuffer);
        }
//        关闭相关流
        fileInputStream.close();
        fileOutputStream.close();
    }
}
